package com.niodata.dp.core.security;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface ResourceAuthMapper {

  @Insert("insert into RESOURCE_AUTH (RES_ID,TARGET_ID,TARGET_NAME,TARGET_TYPE,"
        + "ACCESS_TYPE,AUTH_TIME,RES_TYPE,"
        + "RES_PATH)"
        + "VALUES("
        + "#{auth.resourceId},#{auth.targetId},#{auth.targetName},#{auth.targetType},"
        + "#{auth.accessType},#{auth.authTime},#{auth.resType},"
        + "#{auth.resPath}"
        + ")")
  void addResAuth(@Param("auth") ResourceAuth resourceAuth);

  void deleteResAuth(@Param("resId") String resId, @Param("targetId") long targetId,
        @Param("targetType") String targetType,
        @Param("accessType") String accessType);

  @Delete("delete from RESOURCE WHERE OWNER_ID=#{userId} and OWNER_TYPE='USER'")
  void deleteResOfUser(@Param("userId") long userId);

  @Delete("delete from RESOURCE WHERE OWNER_ID=#{groupId} and OWNER_TYPE='GROUP'")
  void deleteResOfGroup(@Param("groupId") long groupId);

  @Delete("delete from RESOURCE_AUTH  WHERE RES_ID=#{resId}")
  void deleteResAuthOfResource(@Param("resId") String resId);

  @Delete("delete from RESOURCE_AUTH  WHERE RES_PATH=#{path} and RES_TYPE=#{type}")
  void deleteResAuthByPath(@Param("path") String path, @Param("type") String type);

  List<ResourceAuth> getResourceAuth(@Param("resId") String resId,
        @Param("targetId") long targetId,
        @Param("targetType") String targetType,
        @Param("resType") String resType,
        @Param("accessType") String accessType);

  List<ResourceAuth> getUserManagedResourceAuth(@Param("resType") String resType,
        @Param("userId") long userId, @Param("targetType") String targetType,
        @Param("targetId") String targetId, @Param("offset") int offset,
        @Param("count") int count);

  List<ResourceAuth> getUserAuthorizedResourceAuth(@Param("resType") String resType,
        @Param("userId") long userId, @Param("accessType") String accessType);
}
